#ifndef _PRESENTATION_SWF_HPP_
#define _PRESENTATION_SWF_HPP_

// presentation swf type

#include "presentation.hpp"
#include "generator.hpp"
#include "mingpp.h"
#include "gen_text_swf.hpp"
#include "gen_figure_swf.hpp"
#include "gen_video_swf.hpp"
#include "gen_inline_swf.hpp"

class presentation_swf:public xlider_presentation {
public:
	presentation_swf(string pf):xlider_presentation(pf){}
	
	void assign_generators() {
		for(int i=0;i<item.size();i++) {
			if(item[i]->type=="text") {
				item[i]->set_generator(new text_swf); continue;
			}
			
			if(item[i]->type=="figure"){
				item[i]->set_generator(new figure_swf); continue;
			}
			
			if(item[i]->type=="movie"){
				item[i]->set_generator(new video_swf); continue;
			}

			if(item[i]->type=="inline"){
				item[i]->set_generator(new inline_swf); continue;
			}
		}
	}
	
	void create_slide(){
		vector<SWFDisplayItem*> swfitem;
		int r,g,b;
		
		string act(cat(get_scriptpath(slide_script)));
		act.append("max_item=");
		act.append(as_string(item.size())+";");
		
		assign_generators();

		Ming_useSWFVersion(6);

		// control buttons etc...
		SWFMovie* movie=new SWFMovie;

		movie->setDimension(slide_width,slide_height);
		
		// rectangle for background....
		SWFShape *bgbox=new SWFShape;
		
		SWFSprite* slidebtn=new SWFSprite;
		convert_color(background_color,r,g,b);
		bgbox->setRightFill(bgbox->addSolidFill(r,g,b));
		bgbox->drawLine(slide_width,0);
		bgbox->drawLine(0,slide_height);
		bgbox->drawLine(-slide_width,0);
		bgbox->drawLine(0,-slide_height);
		slidebtn->add(bgbox);
		slidebtn->nextFrame();
		
		// interaction buttons
		SWFDisplayItem *bgbtn=movie->add(slidebtn);
		bgbtn->setName("slide_button");
		
		if(background_image!=""){
			string bgimg=get_imagepath(background_image);
			SWFSprite *bg=new SWFSprite;
			SWFBitmap *img=new SWFBitmap(bgimg.c_str());
			SWFDisplayItem *di=bg->add(img);
			di->scale(background_scale);
			bg->nextFrame();
			movie->add(bg);
		}

		// now draw all items
		double nexttop=0.0;
		for(int i=0;i<item.size();i++){
			double ind=margin_left+item[i]->get_indent();
			double upd=margin_top+nexttop;
			
			// CHECK HERE!
//			item[i]->set_textwidth(1.0-margin_right-ind);

			SWFSprite* bl=(SWFSprite*)item[i]->generate();
			if(bl) {
				SWFDisplayItem* di;
				di=movie->add(bl);
				
				if(!item[i]->floating)item[i]->set_pos(ind,upd);
				di->moveTo(item[i]->get_x(),item[i]->get_y());

				string iname("item_");
				iname.append(as_string(i));
				di->setName(iname.c_str());
				if(!item[i]->floating)nexttop+=item[i]->get_height();
			}
		}

		movie->add(new SWFAction(act.c_str())); 
		string slideswf=slide_names[slide_idx]+".swf";
		movie->save(slideswf.c_str()); // save to swf file
		delete movie;
		for(int i=0;i<item.size();i++) item[i]->clean_garbage();
		garbage.push_back(slideswf);
	}

	// decorations.....?
	void combine_slides(){
		if(slide_names.size()==0)return;

		cerr<<"using interaction script: "<<get_scriptpath(inter_script)<<"\n";
		string actscript(cat(get_scriptpath(inter_script)));
		string act;
		
		int r,g,b;
		int maxslide=0;
		
		char st[64];
		vector<string> arname;
		
		// action script
		act.append("var slide=new Array();");
		act.append("var current_slide=0;");
		act.append("var max_slide=0;");
		
		for(int i=0;i<slide_names.size();i++){
			sprintf(st,"sl%d",i);
			arname.push_back(st);
			if(slide_names[i]=="") continue;
			sprintf(st,"slide[%d]=sl%d;",i,i);
			act.append(st);
			maxslide++;
		}

		sprintf(st,"max_slide=%d;",maxslide);

		act.append(st);
		act.append(actscript);

		Ming_useSWFVersion(6);

		SWFMovie *movie=new SWFMovie;
		movie->setDimension(slide_width,slide_height);
		convert_color(background_color,r,g,b);
		movie->setBackground(0,0,0);
		movie->add(new SWFAction(act.c_str()));
		
		vector<SWFDisplayItem*> slides;
		int totslide=0;
		for(int i=0;i<slide_names.size();i++) {
			if(slide_names[i]=="") continue;
			string swfile=slide_names[i]+".swf";
			assert(file_exist(swfile), "failed in creating swf file", swfile);
			SWFPrebuiltClip *spc=new SWFPrebuiltClip(swfile.c_str());
			SWFDisplayItem* di=movie->add(spc);
			di->moveTo(0.0,3000.0);
			di->setName(arname[i].c_str());
			slides.push_back(di);
			totslide++;
			delete spc;
		}
		
		//---- invisible control buttons -----
		
		SWFSprite* psp=new SWFSprite;
		SWFShape *pbtn=new SWFShape;
		SWFFill *pfill=pbtn->addSolidFill(0,0,0,0);

		pbtn->setRightFill(pfill);
		pbtn->drawCircle(slide_width*0.15);
		psp->add(pbtn);
		psp->nextFrame();
		SWFDisplayItem *prevbtn=movie->add(psp);
		prevbtn->moveTo(0,slide_height);
		prevbtn->setName("prev_button");	
		SWFSprite* nsp=new SWFSprite;
		nsp->add(pbtn);
		nsp->nextFrame();
		SWFDisplayItem *nextbtn=movie->add(nsp);
		nextbtn->setName("menu_button");
		
		// --------------save it-------------
		
		slides[0]->moveTo(0.0, 0.0);
		movie->save((basename(filename,true)+".swf").c_str(),9);
		delete movie;
		clean_garbage();
		cerr << "Number of slides: " << totslide << "\n";
	}
		
};

#endif
